package com.stedu.controller;

import com.stedu.bean.Course;
import com.stedu.bean.RespBean;
import com.stedu.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    //增加
    @PostMapping("/insert")
    public RespBean insert(@RequestBody @Validated Course course){
        //添加
        if (courseService.insert(course)){
            return  RespBean.ok("添加成功~~~");
        }
        else {
            return RespBean.error("添加失败，请重新添加");
        }
    }
    //根据id删除
    @DeleteMapping("/{cid}")
    public RespBean delete(@PathVariable("cid") Integer cid){
        //删除
        if (courseService.delete(cid)){
            return RespBean.ok("删除成功~~~");
        }else{
            return RespBean.error("无法删除~~~");
        }
    }
    //根据id修改
    @PutMapping("/update")
    public RespBean update(@RequestBody @Validated Course course){
        //修改
        if (courseService.update(course)){
            return RespBean.ok("修改成功~~~");
        }
        else {
            return RespBean.error("修改失败，请重新修改");
        }
    }

    //根据sid查
    @GetMapping("/{cid}")
    public RespBean selectBySid(@PathVariable("cid") Integer cid){
        Course course = courseService.selectByCid(cid);
        return RespBean.ok("", course);
    }

    @GetMapping
    public RespBean selectAll(){
        List<Course> collegeList = courseService.selectAll();
        return RespBean.ok("", collegeList);
    }
}
